package cn.tt.tuantuanerp.controller;

import cn.tt.tuantuanerp.config.OSSConfig;
import cn.tt.tuantuanerp.service.impl.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 需要上传图片到阿里云   需自定义文件夹名(自动默认test/)
 *
 * @author gudiwen
 * @version 0.0.1
 */
@RestController
public class OSSController {

    @Autowired
    Upload upload;

    @RequestMapping("/{ossPath}/upload")
    public String  GetImg(@RequestParam("pic") MultipartFile pic , @PathVariable String ossPath) throws Exception {
        //得到唯一文件名
        String fileName = pic.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        String imgPath = ossPath+"/"+fileName;
        upload.uploadImage(imgPath, pic);
        return "https://tuantuan-img.oss-cn-hangzhou.aliyuncs.com/"+imgPath;
    }
    @RequestMapping("/remove")
    public void remove(String imgPath){
//        System.out.println(imgPath);
        //删除文件
        upload.deleteFile(imgPath);
    }
}
